//
//  LoginViewController.swift
//  XMGG
//
//  Created by iDev on 6/30/17.
//  Copyright © 2017 iDev. All rights reserved.
//

import UIKit
import SVProgressHUD

class LoginViewController: UIViewController {
 
    var loginView:LoginView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let username = UserDefaults.standard.string(forKey: kNSDefaultUsername)
        let pwd = UserDefaults.standard.string(forKey: kNSDefaultPwd)
        NotificationCenter.default.addObserver(self, selector: #selector(loginResult(info:)), name: NSNotification.Name(rawValue: "loginResult"), object: nil)
        loginView = LoginView(frame: UIScreen.main.bounds,name:username)
        loginView.backgroundColor = UIColor(patternImage: UIImage(named: "content_bgland")!)
        view.addSubview(loginView)
        if  username==nil || pwd==nil {
            userNotLogin()
        }
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if(UserDefaults.standard.bool(forKey: kNSDefaultIsLogin)){
            let username = UserDefaults.standard.string(forKey: kNSDefaultUsername)
            let pwd = UserDefaults.standard.string(forKey: kNSDefaultPwd)
            let modle = LoginModle(tu_account: username, tu_password: pwd, tu_loginDate: getCurrentTime())
            userRequestLogin(modle: modle)
        }
    }
    
    func userLogined(){
        UserDefaults.standard.set(true, forKey: kNSDefaultIsLogin)
        let mainVC = MainViewController()
        mainVC.modalTransitionStyle = .coverVertical
        navigationController?.pushViewController(mainVC, animated: true)
    }
    func userNotLogin(){
        UserDefaults.standard.set(false, forKey: kNSDefaultIsLogin)
    }
    
    func userRequestLogin(){
        firstRespondView?.resignFirstResponder()
        let modle = loginView.getLoginModle()
        if modle.isLeagal(){
            SVProgressHUD.show(withStatus: "登录中")
            modle.loginRequest()
        }else{
            let alert = UIAlertController(title: nil, message: "用户名或密码不合法", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
    func userRequestLogin(modle: LoginModle){
        let modle = loginView.getLoginModle()
        if modle.isLeagal(){
            SVProgressHUD.show(withStatus: "登录中")
            modle.loginRequest()
        }else{
            let alert = UIAlertController(title: nil, message: "用户名或密码不合法", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    func loginResult(info:Notification){
        let result = info.object as! Bool
        if result {
            SVProgressHUD.showSuccess(withStatus: "登录成功")
            userLogined()
        }else{
            let reason = info.userInfo?["reason"] as? String
            UserDefaults.standard.set(nil, forKey: kNSDefaultPwd)
            SVProgressHUD.showError(withStatus: reason ?? "登录失败")
        }
    }
    
    func registerBtnClick() {
        navigationController?.pushViewController(RegisterViewController(), animated: true)
    }
    
}
